<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>注册</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="js/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="css/admin.css" media="all">
  <link rel="stylesheet" href="css/login.css" media="all">
</head>
<body>


  <div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">
    <div class="layadmin-user-login-main">
      <div class="layadmin-user-login-box layadmin-user-login-header">
        <h2>用户注册</h2>
        <p> </p>
      </div>
      <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
        <div class="layui-form-item">
          <label class="layadmin-user-login-icon layui-icon layui-icon-cellphone" for="LAY-user-login-cellphone"></label>
          <input type="text" name="email" id="LAY-user-login-cellphone" lay-verify="email" placeholder="邮箱" class="layui-input">
        </div>
        
        <div class="layui-form-item">
          <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
          <input type="password" name="password" id="LAY-user-login-password" lay-verify="pass" placeholder="密码" class="layui-input">
        </div>
        <div class="layui-form-item">
          <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-repass"></label>
          <input type="password" name="repass" id="LAY-user-login-repass" lay-verify="required" placeholder="确认密码" class="layui-input">
        </div>
        <div class="layui-form-item">
          <div class="layui-row">
            <div class="layui-col-xs7">
              <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="LAY-user-login-vercode"></label>
              <input type="text" name="code" id="LAY-user-login-vercode" lay-verify="required" placeholder="验证码" class="layui-input">
            </div>
            <div class="layui-col-xs5">
              <div style="margin-left: 10px;">
                <button type="button" class="layui-btn layui-btn-primary layui-btn-fluid" id="LAY-user-getemailcode">获取验证码</button>
              </div>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-reg-submit">注 册</button>
        </div>
        <div class="layui-trans layui-form-item layadmin-user-login-other">
          <a href="login.html" class="layadmin-user-jump-change layadmin-link layui-hide-sm layui-show-xs-inline-block">已有帐号登入</a>
        </div>
      </div>
    </div>
    
    <div class="layui-trans layadmin-user-login-footer">
      
      <p> </p>
    </div>

  </div>

  <script src="js/layui/layui.js"></script>  
  <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script>
  	$.ajaxSetup({
		complete:function(XMLHttpRequest,textStatus){
			console.log(XMLHttpRequest,textStatus);
             if(XMLHttpRequest.status == 401){
                 //会话失效，跳转到登录页
                 window.location.replace("login.html");
             }else if(XMLHttpRequest.status == 303){
                 alert(XMLHttpRequest.responseText);
             }else if(XMLHttpRequest.status == 500){
                 alert("请求失败，请稍后再试！");
             }
          }
	});
	
  function checkEmail( _callback ){
	var email = $("#LAY-user-login-cellphone").val();
	if(!email){
		return;
	}
	//校验邮箱是否存在接口
      $.ajax({
        url: '/api/user/isRegistered/'+email
        ,data: {}
        ,success: function(res){
			if(res){
				layer.msg('邮箱已存在，请重新录入！', {
					offset: '15px'
					,icon: 2
					,time: 2000
				  });
				  $("#LAY-user-login-cellphone").val("");
				  $("#LAY-user-login-cellphone").focus();
				return;
			}
			if(_callback)
				_callback();
        }
      });
  }
  
  function checkCode(email,code, _callback ){
	//校验验证码接口
      $.ajax({
        url: '/api/code/validate/'+email+'/'+code
        ,data: {}
        ,success: function(res){
			if(res == 1){
				layer.msg('验证码录入错误，请重新录入！', {
					offset: '15px'
					,icon: 2
					,time: 2000
				  });
				return;
			}else if(res == 2){
				layer.msg('验证码已失效，请重新获取后录入！', {
					offset: '15px'
					,icon: 2
					,time: 2000
				  });
				$("#LAY-user-login-vercode").val("");
				return;
			}
          _callback();
        }
      });
  }
  
  layui.use(['form'], function(){
    var form = layui.form;

    form.render();

    //提交
    form.on('submit(LAY-user-reg-submit)', function(obj){
      var field = obj.field;
      
      //确认密码
      if(field.password !== field.repass){
        return layer.msg('两次密码输入不一致');
      }
	  
	  checkEmail(function(){
		  checkCode(field.email, field.code, function(){
			  //注册接口
			  $.ajax({
				url: '/api/user/register/'+field.email+'/'+field.password+'/'+field.code
				,data: field
				,success: function(res){
					if(!res){
						layer.msg('注册失败，请稍后再试！', {
							offset: '15px'
							,icon: 2
							,time: 2000
						  });
						return;
					}
				  layer.msg('注册成功', {
					offset: '15px'
					,icon: 1
					,time: 2000
				  }, function(){
					window.location = 'login.html'; //跳转到登入页
				  });
				}
			  });
		  });
      });
      return false;
    });
	
	
	//邮箱变更时，提示邮箱是否存在
	$("#LAY-user-login-cellphone").on("change",function(){
		checkEmail(false);
    });
	
	$("#LAY-user-getemailcode").on("click",function(){
		var email = $("input[name=email]").val();
		if(!email){
			layer.msg('请先录入邮箱！', {
				offset: '15px'
				,icon: 2
				,time: 2000
			  });
			  $("input[name=email]").focus();
			return false;
		}
		//请求接口
      $.ajax({
        url: '/api/code/create/' + $("input[name=email]").val()
        ,data: {}
        ,success: function(res){
			get_countdown("#LAY-user-getemailcode");
		
          layer.msg('验证码已发送至您的邮箱，请前往邮箱中查看！', {
            offset: '15px'
            ,icon: 1
            ,time: 2000
          });
        }
      });
	});
  });
  
  function get_countdown(elementName) {
			var t = 59;//倒计总秒数
			var time = 59;//当前秒数
			if (time == t) { //如果不加入该判断，则会出现在倒计时期间不断的点击发生不断的加快（其实就是你点了多少次就重复多少次该函数）的问题，用setTimeout（）方法不加此判断也是一样的
				var time1 = setInterval(function() {
					  if (time == 0) {
						  $(elementName).html("获取验证码");
						  $(elementName).removeAttr("disabled");
						  time = t;
						  clearInterval(time1);
						  $(elementName).addClass("color-green").removeClass("color-gray");
					  } else {
						  $(elementName).attr("disabled","true");
						  $(elementName).html(time + "重新获取");
						  $(elementName).removeClass("color-green").addClass("color-gray");
						  time--;
					  }
						console.log(time);
				}, 1000);
			}
		}
//elementName为按钮的class名
  </script>
</body>
</html>